from . import app_register
from conn_mysql import MysqlApi
from flask import jsonify, request, render_template, redirect

@app_register.route('/api/v3/register', methods = ["POST"])

def api_register():
    
    data_post = request.form

    info = dict(
        company_name = data_post.get('company_name'),
        product_name = data_post.get('product_name'),
        need = data_post.get('need'),
        linkman = data_post.get('linkman'),
        phone = data_post.get('phone'),
        party = data_post.get('party'),
        pass_word = data_post.get('pass_word'),
        openid = data_post.get('openid'),
    )

    with MysqlApi() as MA:

        result = MA.insert_register(info)


    return jsonify(result)


@app_register.route('/register', methods = ["POST"])

def register():
    
    data_post = request.form

    info = dict(
        company_name = data_post.get('company_name'),
        product_name = data_post.get('product_name'),
        need = data_post.get('need'),
        linkman = data_post.get('linkman'),
        phone = data_post.get('phone'),
        party = data_post.get('party'),
        pass_word = data_post.get('pass_word'),
        openid = data_post.get('openid'),
    )

    with MysqlApi() as MA:

        result = MA.insert_register(info)

    if result['status'] == 1:#注册成功

        return redirect('/login?msg=注册成功，请登录')

    else:

        if result['errorCode'] == 1062:

            return redirect('/login?msg=一个微信号或手机号只能注册一个账号')

        return redirect('/api/v3/openid_callback')
